package com.quanyan.stadium.service.gameEvent.impl;

import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.place.entity.apiresp.RespCity;
import com.quanyan.place.entity.apiresp.RespCityInfos;
import com.quanyan.place.entity.apiresp.RespDistrict;
import com.quanyan.stadium.api.req.gameEvent.ReqGameEventSearch;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.TbGameEvent;
import com.quanyan.stadium.entity.vo.GameEvent;
import com.quanyan.stadium.entity.vo.GameEventConfig;
import com.quanyan.stadium.enums.GameSystemConfigEnum;
import com.quanyan.stadium.service.GameEventService;
import com.quanyan.stadium.service.commonComponent.BaseService;
import com.quanyan.stadium.service.commonComponent.OfficialGameCache;
import com.quanyan.stadium.service.gameEvent.GameEventSearchService;
import com.quanyan.stadium.transfer.GameConfigTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by lei.w on 2016/10/12.
 */
@Service
public class GameEventSearchServiceImpl implements GameEventSearchService {

    private static final Logger logger = LoggerFactory.getLogger(GameEventSearchServiceImpl.class);

    @Autowired
    private BizGameSystemDao bizGameSystemDao;
    @Autowired
    private BaseService baseService;
    @Autowired
    private OfficialGameCache officialGameCache;
    @Autowired
    private GameEventService gameEventService;
    @Autowired
    private GameConfigTransfer gameEventTransfer;

    @Override
    public GameEventConfig gameEventSearchConfig(int type) {
        GameEventConfig gameEventConfig = officialGameCache.getGameEventConfig(type);
        if (null == gameEventConfig) {
            gameEventConfig = new GameEventConfig();
            List<RespCityInfos> cityInfosList = baseService.queryRespCityInfos();
            RespDistrict respDistrict;
            List<RespDistrict> districtList;
            if (ObjectUtil.isNotObjectEmpty(cityInfosList) && cityInfosList.size() > 0 ) {
                for(RespCityInfos respCityInfos : cityInfosList){
                    respDistrict = new RespDistrict();
                    respDistrict.setId(0);
                    respDistrict.setCityId(respCityInfos.getRespCity().getId());
                    respDistrict.setName("全"+respCityInfos.getRespCity().getName());

                    districtList = new ArrayList<>();
                    districtList.add(0, respDistrict);
                    districtList.addAll(respCityInfos.getRespDistrict());
                    respCityInfos.setRespDistrict(districtList);
                }

                RespCityInfos cityInfos = new RespCityInfos();
                RespCity respCity = new RespCity();
                respCity.setId(-1);
                respCity.setName("其他");
                cityInfos.setRespCity(respCity);

                respDistrict = new RespDistrict();
                respDistrict.setId(0);
                respDistrict.setCityId(respCity.getId());
                respDistrict.setName("全部");

                districtList = new ArrayList<>();
                districtList.add(0, respDistrict);
                cityInfos.setRespDistrict(districtList);
                cityInfosList.add(cityInfos);
            }

            //城市区域
            gameEventConfig.setCityInfosList(cityInfosList);

            // 栏目
            LinkedHashMap<String, Integer> categoryMap = new LinkedHashMap<>();
            categoryMap.put("全部", 0);

            //个人赛显示羽毛球跟网球
            if (type == GameConstants.PERSONAL_CREATE.intValue()) {
                categoryMap.put("羽毛球", GameConstants.CATEGORY_ID_BADMINTON);
                categoryMap.put("网球", GameConstants.CATEGORY_ID_TENNIS);
            } else {
                categoryMap.put("篮球", GameConstants.CATEGORY_ID_BASKETBALL);
                categoryMap.put("羽毛球", GameConstants.CATEGORY_ID_BADMINTON);
                categoryMap.put("足球", GameConstants.CATEGORY_ID_FOOTBALL);
                categoryMap.put("网球", GameConstants.CATEGORY_ID_TENNIS);
            }

            gameEventConfig.setCategoryMap(categoryMap);

            // 栏目等级
            LinkedHashMap<Integer, Map> categoryLevelMap = new LinkedHashMap<>();

            //无等级限制表示 所有,使用-1标识
//        String[] levels = new String[]{"无等级限制","1-3","4-6","7-8"};
            LinkedHashMap<String, Integer> levelMap1 = new LinkedHashMap<>();
            levelMap1.put("全部", -1);
            if (type != GameConstants.PERSONAL_CREATE.intValue()) {
                levelMap1.put("无等级限制", 0);
            }
            levelMap1.put("3级以下", 1);
            levelMap1.put("4级", 2);
            levelMap1.put("5级", 3);
            levelMap1.put("6级", 4);
            levelMap1.put("7级以上", 5);

            LinkedHashMap<String, Integer> levelMap2 = new LinkedHashMap<>();
            levelMap2.put("全部", -1);
            if (type != GameConstants.PERSONAL_CREATE.intValue()) {
                levelMap2.put("无等级限制", 0);
            }
            levelMap2.put("2.0以下", 6);
            levelMap2.put("2.5", 7);
            levelMap2.put("3.0", 8);
            levelMap2.put("3.5", 9);
            levelMap2.put("4.0以上", 10);

            Map<String, Integer> levelMap3 = new HashMap<>();
            levelMap3.put("全部", -1);

            // 特殊处理
            categoryLevelMap.put(0, levelMap3);
            // 类型为2 即个人赛
            if (type == GameConstants.PERSONAL_CREATE.intValue()) {
                categoryLevelMap.put(GameConstants.CATEGORY_ID_BADMINTON, levelMap1);
                categoryLevelMap.put(GameConstants.CATEGORY_ID_TENNIS, levelMap2);
            } else {
                categoryLevelMap.put(GameConstants.CATEGORY_ID_FOOTBALL, levelMap1);
                categoryLevelMap.put(GameConstants.CATEGORY_ID_BASKETBALL, levelMap1);
                categoryLevelMap.put(GameConstants.CATEGORY_ID_BADMINTON, levelMap1);
                categoryLevelMap.put(GameConstants.CATEGORY_ID_TENNIS, levelMap2);
            }
            gameEventConfig.setCategoryLevel(categoryLevelMap);

            if (type != GameConstants.PERSONAL_CREATE.intValue()) {
                // 赛制
                LinkedHashMap<String, Integer> gameSystemTypeMap = new LinkedHashMap<>();
                gameSystemTypeMap.put("全部", 0);
                gameSystemTypeMap.put(GameSystemConfigEnum.CONFIG_GAME_TYPE_SWISS.getLabel(), GameSystemConfigEnum.CONFIG_GAME_TYPE_SWISS.getId());
                gameSystemTypeMap.put(GameSystemConfigEnum.CONFIG_GAME_TYPE_WASHED.getLabel(), GameSystemConfigEnum.CONFIG_GAME_TYPE_WASHED.getId());
                gameSystemTypeMap.put(GameSystemConfigEnum.CONFIG_GAME_TYPE_MIXED.getLabel(), GameSystemConfigEnum.CONFIG_GAME_TYPE_MIXED.getId());
                gameSystemTypeMap.put(GameSystemConfigEnum.CONFIG_GAME_TYPE_CIRCLE.getLabel(), GameSystemConfigEnum.CONFIG_GAME_TYPE_CIRCLE.getId());

                gameEventConfig.setGameSystemTypeMap(gameSystemTypeMap);
            }
            officialGameCache.intiGameEventConfig(gameEventConfig, type);
        }


        return gameEventConfig;
    }

    @Override
    public List<GameEvent> gameEventSearchSequence(ReqGameEventSearch gameEventSearch) {
        if (ObjectUtil.isObjectEmpty(gameEventSearch)) {
            logger.info("gameEventSearchSequence gameEventSearch is null.");
            return Collections.emptyList();
        }

        logger.info("gameEventSearchSequence gameEventSearch info:"+ gameEventSearch.toString());

        boolean gameFlag = false;
        Map<String, Integer> searchMap = new HashMap<>();

        if (-1 == gameEventSearch.getCityId()) {
            gameEventSearch.setCityId(0);
        }

        searchMap.put("cityId", gameEventSearch.getCityId());

        if (gameEventSearch.getDistrictId() != 0) {
            searchMap.put("districtId", gameEventSearch.getDistrictId());
        }

        if (gameEventSearch.getGameSystemType() != 0) {
            searchMap.put("gameSystemType", gameEventSearch.getGameSystemType());
        }

        if (gameEventSearch.getType() != 0) {
            searchMap.put("type", gameEventSearch.getType());
        }

        if (gameEventSearch.getCategoryId() != 0) {
            searchMap.put("categoryId", gameEventSearch.getCategoryId());
        }

        // 赛事等级特殊，边界值为-1
        if (gameEventSearch.getGameLevelType() > -1) {
            Map<String, Double> levelMap = getLevelByType(gameEventSearch.getGameLevelType());

            if (levelMap.get("falg").intValue() == 1) {
                searchMap.put("gameMinLevel", gameEventTransfer.getLevelIdByLevelAndCateId(levelMap.get("min")));
                searchMap.put("gameMaxLevel", gameEventTransfer.getLevelIdByLevelAndCateId(levelMap.get("max")));
            } else {
                searchMap.put("gameLevel", gameEventTransfer.getLevelIdByLevelAndCateId(levelMap.get("min")));
            }
        }

        searchMap.put("pageNo", gameEventSearch.getPageNo());

        if (gameEventSearch.getPageSize() == 0) {
            gameEventSearch.setPageSize(10);
        }
        searchMap.put("pageSize", gameEventSearch.getPageSize());

        return changeListTbGameEvent(bizGameSystemDao.gameEventSearch(searchMap), gameEventSearch);
    }

    private List<GameEvent> changeListTbGameEvent(List<TbGameEvent> tbGameEventList, ReqGameEventSearch gameEventSearch) {

        if(ObjectUtil.isObjectEmpty(tbGameEventList) || tbGameEventList.size() == 0) {
            logger.info("changeListTbGameEvent tbGameEventList is null.");
            return Collections.emptyList();
        }
        boolean userFlag = false;
        List<Integer> userJoinedGameEventIds = null;
        if (gameEventSearch.getUid() > 0) {
            userJoinedGameEventIds = gameEventService.userGameEventIds(gameEventSearch.getUid(), gameEventSearch.getCategoryId());      //用户报名参加的赛事(大球类,或者小球类)
            if (null != userJoinedGameEventIds && userJoinedGameEventIds.size()>0) {
                userFlag = true;
            }
        }
        List<GameEvent> gameEventList = new ArrayList<>();
        try {
            GameEvent gameEvent;
            for (TbGameEvent tbGameEvent : tbGameEventList) {
                gameEvent = officialGameCache.getGameEvent(tbGameEvent.getId());
                gameEvent.setCurrentTeamNum(tbGameEvent.getTeamLimitNum() - tbGameEvent.getInventoryLevel());
                gameEvent.setCurTime(new Date().getTime());
                if (userFlag) {
                    if (userJoinedGameEventIds.contains(tbGameEvent.getId())) {
                        gameEvent.setIsJoin((byte) 1);
                    }
                }
                gameEventList.add(gameEvent);
            }

        } catch (Exception e) {
            logger.error("changeListTbGameEvent exception info:", e);
        }

        return gameEventList;
    }

    private Map<String, Double> getLevelByType(int type) {
        Map<String, Double> map = new HashMap<>();

        double min = 0, max = 0, falg = 0;
        switch (type) {
            case 1:
                falg = 1;
                min = 1;
                max = 3;
                break;
            case 2:
                min = 4;
                break;
            case 3:
                min = 5;
                break;
            case 4:
                min = 6;
                break;
            case 5:
                falg = 1;
                min = 7;
                max = 10;
                break;
            case 6:
                falg = 1;
                min = 1.0;
                max = 2.0;
                break;
            case 7:
                min = 2.5;
                break;
            case 8:
                min = 3.0;
                break;
            case 9:
                min = 3.5;
                break;
            case 10:
                falg = 1;
                min = 4.0;
                max = 10.0;
                break;
            default:
                min = 0;
                max = 0;
        }

        map.put("falg", falg);
        map.put("min", min);
        map.put("max", max);

        return map;
    }
}
